package com.cql.service.business.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cql.common.constant.FoodCircleConstant;
import com.cql.common.core.DO.SysDictDataDO;
import com.cql.common.core.DTO.DictDataDTO;
import com.cql.common.utils.StringUtils;
import com.cql.mapper.SysDictDataMapper;
import com.cql.service.business.SysDictDataService;
import com.cql.service.cache.CommonCacheService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * @author cql
 */
@Service("sysDictDataService")
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictDataDO> implements SysDictDataService {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());


    @Autowired
    private CommonCacheService commonCacheService;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    @Override
    public List<DictDataDTO> listDictDataByType(String dictType) {
        /**
         * 先从redis查询，再从数据库查询
         */
        List<DictDataDTO> result = commonCacheService.getDictDataByType(dictType);
        if (StringUtils.isNotEmpty(result)) {
            return result;
        }

        QueryWrapper<SysDictDataDO> wrapper = new QueryWrapper<>();
        wrapper.eq("dict_type", dictType);
        wrapper.eq("status", 0);
        List<SysDictDataDO> sysDictDataDOS = sysDictDataMapper.selectList(wrapper);

        result = new ArrayList<>();

        for (SysDictDataDO sysDictDataDO : sysDictDataDOS) {
            DictDataDTO d = new DictDataDTO();
            BeanUtils.copyProperties(sysDictDataDO, d);
            result.add(d);
        }


        //存储数据
        commonCacheService.insertDictData(dictType, result);

        return result;
    }

    @Override
    public String selectDictLabel(Long code) {
        if (StringUtils.isNotNull(code)) {
            SysDictDataDO dictDataDO = sysDictDataMapper.selectById(code);
            if (StringUtils.isNull(dictDataDO)) {
                return "";
            }
            return dictDataDO.getDictLabel();
        }
        return "";
    }

    @Override
    public String listDictLabel(List<Long> tools) {
        List<String> labels = new ArrayList<>();
        if (StringUtils.isNotEmpty(tools)) {
            for (Long tool : tools) {
                if (StringUtils.isNotNull(tool)) {
                    SysDictDataDO dictDataDO = sysDictDataMapper.selectById(tool);
                    if (StringUtils.isNotNull(dictDataDO)) {
                        labels.add(dictDataDO.getDictLabel());
                    }
                }
            }
        }
        String join = StringUtils.join(labels, FoodCircleConstant.SEPARATE_CHAR);
        return join;
    }

    @Override
    public Long getDictCode(String label) {
        if (StringUtils.isNotEmpty(label)) {
            QueryWrapper<SysDictDataDO> wrapper = new QueryWrapper<>();
            wrapper.eq("dict_label", label);
            SysDictDataDO dict = sysDictDataMapper.selectOne(wrapper);
            if (StringUtils.isNotNull(dict)) {
                return dict.getDictCode();
            } else {
                logger.warn("数据字典: {} 不存在", label);
                return new Long(-1);
            }
        }
        return new Long(-1);
    }

    @Override
    public List<Long> listDictCode(String tools) {
        List<Long> result = new ArrayList<>();
        if (StringUtils.isNotEmpty(tools)) {
            String[] split = tools.split(FoodCircleConstant.SEPARATE_CHAR);
            for (String s : split) {
                Long dictCode = this.getDictCode(s);
                if (dictCode != -1) {
                    result.add(dictCode);
                }
            }
        }
        return result;
    }
}